package search33;

public class Search {
    public int search(int[] nums, int target) {
        int left=0;
        int right = nums.length-1;

        //二分法基本原理就是在升序序列中，不断缩小边界进行查找，所以这个题就要确定mid左和右两侧如果都是升序就分别在两侧中进行查找
        while(left<=right){
            int mid = (left+right)/2;
            if(target == nums[mid]) return mid;
            if(nums[left]<=nums[mid]){
                //说明左侧是升序，需要在左侧查找
                if(target<=nums[mid] && target>=nums[left]){
                    right = mid - 1;
                }else{
                    //右半部分
                    left = mid+1;
                }
            }else{
                //说明右侧是升序，需要在右侧进行二分查找
                if(target>=nums[mid] && target<=nums[right]){
                    left = mid+1;
                }else{
                    //左半边
                    right = mid - 1;
                }
            }
        }
        return -1;

    }

    public static void main(String[] args) {
        Search search = new Search();
        int[] a = {3, 1};
        System.out.println(search.search(a, 1));
    }

}
